<?php

require_once('Interface.php');
require_once('/../Request.php');
require_once('/../Template/File/LocaleAware.php');
require_once('/../Template/Engine/Smarty.php');

class CodeIslands_Api_Service_Factory_Core implements CodeIslands_Api_Service_Factory_Interface
{
	/**
	 * Internal cache of instantiated objects.
	 *
	 * @var mixed
	 */
	static protected $_objects;
	
	public function __construct()
	{
		error_reporting(E_ALL|E_STRICT);
		ini_set('display_errors', 1);

		//set_include_path(APPLICATION_PATH . '/library' . PATH_SEPARATOR . get_include_path());
	}
	
	protected function createService($service_name)
	{
		$service = null;
		switch($service_name)
		{
			case 'Request':
				$service = new CodeIslands_Api_Service_Request();
				break;
				
			case 'TemplateEngine':
				$service = new CodeIslands_Api_Service_Template_Engine_Smarty(null, $this->createService('TemplateFile'));
				break;
				
			case 'TemplateFile':
				$service = new CodeIslands_Api_Service_Template_File_LocaleAware();
				break;
				
			default:
				$service = new $service_name;
		}	

		return $service;
	}
	
	public function getService($service_name)
	{
		// Return the service if it has already been instantiated
		if (isset(self::$_objects[$service_name]))
			return self::$_objects[$service_name];

		$service = $this->createService($service_name);
		
		return self::$_objects[$service_name] = $service;
	}
}